package com.luck.easyfoodbatch.custom;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.adapter.ShareAdapter;
import com.luck.easyfoodbatch.fragment.ShopDetailsFragment;
import com.luck.easyfoodbatch.util.FileUtils;
import com.luck.easyfoodbatch.util.T;
import com.mob.tools.utils.UIHandler;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

import static android.R.attr.action;

/**
 * Created by Administrator on 2017/4/17.
 */

public class SharePopupWindows extends PopupWindow implements PlatformActionListener, Handler.Callback ,ShopDetailsFragment.CallBackValue{
    private Context mContext;
    String share_title;
    String share_text;
    String share_url;
    private static final int MSG_ACTION_CCALLBACK = 2;
    public SharePopupWindows(final Context mContext, final String share_title, final String share_text, final String share_url, View parent) {
        super(mContext);
        this.mContext = mContext;
        this.share_text=share_text;
        this.share_title=share_title;
        this.share_url=share_url;
        View view = View
                .inflate(mContext, R.layout.item_shap_popupwindows, null);
        final RelativeLayout rl_root = (RelativeLayout) view.findViewById(R.id.rl_root);
        final LinearLayout ll_popup = (LinearLayout) view.findViewById(R.id.ll_popup);
        GridView mGridView = (GridView) view.findViewById(R.id.mGridView);
        ShareAdapter adapter = new ShareAdapter(mContext);
        mGridView.setAdapter(adapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i) {
                    case 0:
                        T.showToast(mContext, "正在启动QQ，请稍候...");
                        qq(share_title, share_text, share_url);
                        break;
                    case 1:
                        T.showToast(mContext, "正在启动微信，请稍候...");
                        wechat(share_title, share_text, share_url);
                        break;
                    case 2:
                        T.showToast(mContext, "正在启动微信，请稍候...");
                        wechatmoments(share_title, share_text, share_url);
                        break;
                }
                dismiss();
            }
        });
        TextView tv_canel = (TextView) view.findViewById(R.id.tv_canel);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setBackgroundDrawable(new BitmapDrawable());
        setFocusable(true);
        setOutsideTouchable(true);
        setContentView(view);
        showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        update();
        final PopupWindow popupWindow = this;
        tv_canel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hidePopupWindows(ll_popup, popupWindow);
            }
        });
        ll_popup.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.popshow_anim));
        rl_root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hidePopupWindows(ll_popup, popupWindow);
            }
        });
    }


    /**
     * 关闭 PopupWindows
     *
     * @param view
     * @param popupWindow
     */
    private void hidePopupWindows(View view, final PopupWindow popupWindow) {
        Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.pophidden_anim);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                popupWindow.dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        view.startAnimation(animation);
    }


    /**
     * 分享微信
     */
    private void wechat(String share_title, String share_text, String share_url) {
        Platform.ShareParams sp = new Platform.ShareParams();
        sp.setTitle(share_title);
        sp.setText(share_text);
        sp.setImagePath(FileUtils.IMAGE_PATH + "logo.png");
        sp.setShareType(Platform.SHARE_TEXT);
        sp.setShareType(Platform.SHARE_WEBPAGE);
        sp.setUrl(share_url);
        Platform qq = ShareSDK.getPlatform(Wechat.NAME);
        qq.setPlatformActionListener(this);
        qq.share(sp);
    }

    /**
     * 分享微信朋友圈
     */
    private void wechatmoments(String share_title, String share_text, String share_url) {
        Platform.ShareParams sp = new Platform.ShareParams();
        sp.setTitle(share_title);
        sp.setText(share_text);
        sp.setUrl(share_url);
        sp.setImagePath(FileUtils.IMAGE_PATH + "logo.png");
        sp.setShareType(Platform.SHARE_TEXT);
        sp.setShareType(Platform.SHARE_WEBPAGE);
        Platform qq = ShareSDK.getPlatform(WechatMoments.NAME);
        qq.setPlatformActionListener(this);
        qq.share(sp);
    }

    /**
     * 分享QQ
     */
    private void qq(String share_title, String share_text, String share_url) {
        Platform.ShareParams shareParams = new Platform.ShareParams();
        shareParams.setTitle(share_title);
        shareParams.setTitleUrl(share_url); // 标题的超链接
        shareParams.setText(share_text);
        shareParams.setComment("我对此分享内容的评论");
        shareParams.setSite("易食批");
        shareParams.setImagePath(FileUtils.IMAGE_PATH + "logo.png");
        shareParams.setSiteUrl(share_url);
        Platform qq = ShareSDK.getPlatform(mContext, QQ.NAME);
        qq.setPlatformActionListener(this);
        qq.share(shareParams);
    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        // 成功
        Message msg = new Message();
        msg.what = MSG_ACTION_CCALLBACK;
        msg.arg1 = 1;
        msg.arg2 = action;
        msg.obj = platform;
        UIHandler.sendMessage(msg, this);
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        // 失敗
        // 打印错误信息,print the error msg
        throwable.printStackTrace();
        // 错误监听,handle the error msg
        Message msg = new Message();
        msg.what = MSG_ACTION_CCALLBACK;
        msg.arg1 = 2;
        msg.arg2 = action;
        msg.obj = throwable;
        UIHandler.sendMessage(msg, this);
    }

    @Override
    public void onCancel(Platform platform, int i) {
        // 取消
        Message msg = new Message();
        msg.what = MSG_ACTION_CCALLBACK;
        msg.arg1 = 3;
        msg.arg2 = action;
        msg.obj = platform;
        UIHandler.sendMessage(msg, this);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.arg1) {
            case 1: {
                // 成功
                T.showToast(mContext, "分享成功");
            }
            break;
            case 2: {
                // 失败
                T.showToast(mContext, "分享失败");
            }
            break;
            case 3: {
                // 取消
                T.showToast(mContext, "分享取消");
            }
            break;
        }
        return false;
    }

    @Override
    public void SendMessageValue(String strValue) {
        share_text = strValue;
    }
}
